package beanstao.util.store.presenter;

import beanstao.util.Sber;
import beanstao.util.Strings;
import beanstao.util.id.DId;
import beanstao.util.id.Id;
import beanstao.util.response.PD;
import beanstao.util.response.PDs;
import beanstao.util.store.IQD;
import beanstao.util.store.dal.XmlDal;
import beanstao.util.store.domainobject.DomainList;
import beanstao.util.store.domainobject.XmlDomain;
import beanstao.util.store.domainobject.fields.PublicField;


public abstract class AbstractXMLPresenter extends AbstractPresenter implements IPresenter
{

	protected abstract XmlDal<XmlDomain> getDal();

	@Override
	public PD all(IQD data)
	{
		return PDs.builder(this.getDal().selectAll());
	}

	@Override
	public PD count()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PD count(IQD data)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PD create(IQD data)
	{
		XmlDomain domain = this.getDal().newBlankDomain();
		domain.fromRequest(data);
		return PDs.builder(this.getDal().create(domain));
	}

	@Override
	public PD delete(IQD data)
	{
		XmlDomain domain = this.getDal().newBlankDomain();
		domain.fromRequest(data);
		return PDs.builder(this.getDal().delete(domain));
	}

	@Override
	public PD exists(IQD data)
	{
		XmlDomain domain = this.getDal().newBlankDomain();
		domain.fromRequest(data);
		return PDs.builder(this.getDal().exists(domain.getId()));
	}

	@Override
	public PD idName(IQD data)
	{
		Id id = DId.ME(data.get(PublicField.ID).$V().toString());
		PD data2 = PDs.builder(this.getDal().select(id));
		XmlDomain domain = (XmlDomain) data2.$Data();

		String name = (String) domain.get("name");
		if (Strings.isEmpty(name))
			return PDs.builder("");
		return PDs.builder(name);
	}

	@Override
	public PD idNameOfAll(IQD data)
	{
		DomainList<XmlDomain> ctlist = this.getDal().selectAttributeOfAll("id");
		for (Object xmlDomain : ctlist)
		{
			XmlDomain domain = (XmlDomain) xmlDomain;
			String xmlDomainName = this.getDal().queryNameById(domain.getId());
			domain.put("name", xmlDomainName);
		}
		return PDs.builder(ctlist);
	}

	@Override
	public PD idOfAll(IQD data)
	{
		return PDs.builder(this.getDal().selectAttributeOfAll("id"));
	}

	@Override
	public PD like(IQD data)
	{
		Sber xpath = Sber.ME("//" + data.getQueryType().toLowerCase() + "[contains(id,'')");
		for (String key : data.getKeys())
			xpath.append("and contains(@" + key + ",'" + data.get(key).$V() + "')]");

		return PDs.builder(this.getDal().select(null, xpath.toString()));
	}

	@Override
	public PD page(IQD data)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PD remove(IQD data)
	{
		XmlDomain domain = this.getDal().newBlankDomain();
		domain.fromRequest(data);
		return PDs.builder(this.getDal().remove(domain));
	}

	@Override
	public PD removeAll(IQD data)
	{
		return PDs.builder(this.getDal().removeAll(DId.ME("system")));
	}

	@Override
	public PD single(IQD data)
	{
		XmlDomain domain = this.getDal().newBlankDomain();
		domain.fromRequest(data);
		return PDs.builder(this.getDal().select(domain.getId()));
	}

	@Override
	public PD update(IQD data)
	{
		XmlDomain domain = this.getDal().newBlankDomain();
		domain.fromRequest(data);
		XmlDomain newDomain = this.getDal().update(domain);
		if ((newDomain != null) && !newDomain.isEmpty())
			return PDs.builder(true);
		else
			return PDs.builder(false);
	}

	@Override
	public PD where(IQD data)
	{
		String xpath = data.toXPath();
		return PDs.builder(this.getDal().where(xpath));

	}

}
